package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HChangeOrder;
import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.TProject;
import com.zhongwang.mms.module.half.dao.HChangeOrderDao;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.model.HChangeOrderModel;
import com.zhongwang.mms.module.half.model.HInventoryModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.purchase.dao.PurChangeDao;
import com.zhongwang.mms.module.purchase.model.ChangeOrderQueryModel;
import com.zhongwang.mms.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description:串料申请
 * @author: th
 * @time: 2020/4/27 10:43
 */
@Service
public class ChangeApplyService {

    @Autowired
    private HChangeOrderDao hChangeOrderDao;
    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private CreateOrderCodeService createOrderCodeService;
    @Autowired
    private PurChangeDao purChangeDao;

    /**
     * 串料申请页面加载数据
     *
     * @param queryModel
     * @return
     */
    public List<HChangeOrderModel> selectChangeOrderListByQuery(ChangeOrderQueryModel queryModel) {

        return hChangeOrderDao.selectChangeOrderListByQuery(queryModel);
    }

    /**
     * 串料申请添加页面选择库存加载数据
     *
     * @param query
     * @return
     */
    public List<HInventoryModel> inventoryList(HInventoryQueryModel query) {

        query.setItyMatType(1);//外购件
        query.setItyStockType(2);//项目在库
        return hInventoryDao.selectChangeApplyHInventoryByQuery(query);
    }

    /**
     * 保存串料申请
     * @param hChangeOrder
     * @return
     */
    @Transactional
    public Integer saveChangeOrder(HChangeOrder hChangeOrder) {

        //根据主键获取库存信息
        HInventory hInventory = hInventoryDao.selectAllById(hChangeOrder.getCeoUseItyId());
        hChangeOrder.setCeoAppPersonId(ShiroUtils.getUser().getId());
        hChangeOrder.setCeoItyType("h");
        hChangeOrder.setCeoUseMatCode(hInventory.getItyMatCode());
        hChangeOrder.setCeoAppyStatus(1);
        hChangeOrder.setCeoAppDate(new Date());
        if(hChangeOrder.getCeoId() == null){
            //更改库存可用数量
            hInventory.setItyAvaNum(hInventory.getItyAvaNum() - hChangeOrder.getCeoNeedNum());
            hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
            hChangeOrder.setCeoCode(createOrderCodeService.getChangeOrderCode());
            hChangeOrderDao.insertSelective(hChangeOrder);
        } else if (hChangeOrder.getCeoId() != 0){
            //更改库存可用数量
            HChangeOrderModel hChangeOrderModel = hChangeOrderDao.getAllByPrimary(hChangeOrder.getCeoId());
            hInventory.setItyAvaNum(hInventory.getItyAvaNum() + hChangeOrderModel.getCeoNeedNum() - hChangeOrder.getCeoNeedNum());
            hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
            hChangeOrderDao.updateSelective(hChangeOrder);
        }
        return 1;
    }

    /**
     * 提交串料申请
     * @param hChangeOrder
     * @return
     */
    public int submitApply(HChangeOrder hChangeOrder) {

        hChangeOrder.setCeoAppyStatus(2);
        return hChangeOrderDao.updateStatusByPrimary(hChangeOrder);
    }

    /**
     * 删除串料申请
     * @param hChangeOrder
     * @return
     */
    public int removeApply(HChangeOrder hChangeOrder) {
        HChangeOrderModel hChangeOrderModel = hChangeOrderDao.getAllByPrimary(hChangeOrder.getCeoId());
        HInventory hInventory = hInventoryDao.selectAllById(hChangeOrderModel.getCeoUseItyId());
        hInventory.setItyAvaNum(hChangeOrderModel.getCeoNeedNum() + hInventory.getItyAvaNum());
        hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
        return hChangeOrderDao.deleteByPrimaryKey(hChangeOrder.getCeoId());
    }

    /**
     * 查询选择的库存
     * @param ityId
     * @return
     */
    public HChangeOrder getHInventoryByItyId(Integer ityId) {

        HInventory hInventory = hInventoryDao.selectAllById(ityId);
        TProject tProject = purChangeDao.getTProjectByProCode(hInventory.getItyProCode());
        HChangeOrder hChangeOrder = new HChangeOrder();
        hChangeOrder.setCeoUseItyId(hInventory.getItyId());
        if (null != tProject) {
            hChangeOrder.setCeoUseStatus(tProject.getProProductionStatus());
        }
        hChangeOrder.setCeoUseProCode(hInventory.getItyProCode());
        hChangeOrder.setCeoUseProName(hInventory.getItyProName());
        hChangeOrder.setCeoUseMatName(hInventory.getItyMatName());
        hChangeOrder.setCeoUseMatCode(hInventory.getItyMatCode());
        hChangeOrder.setCeoUseNum(hInventory.getItyAvaNum());
        hChangeOrder.setCeoUseUnit(hInventory.getItyUnit());
        hChangeOrder.setCeoNeedUnit(hInventory.getItyUnit());
        hChangeOrder.setCeoUseWhiId(hInventory.getItyWhiId());
        hChangeOrder.setCeoUsePosition(hInventory.getItyPosition());

        return hChangeOrder;

    }

    public HChangeOrderModel getChangeModel(String ceoId) {

        HChangeOrderModel hChangeOrderModel = hChangeOrderDao.getHChangeOrderByCeoId(ceoId);
        HInventory hInventory = hInventoryDao.selectAllById(hChangeOrderModel.getCeoUseItyId());
        hChangeOrderModel.setCeoUseNum(hInventory.getItyAvaNum() + hChangeOrderModel.getCeoNeedNum());
        return hChangeOrderModel;
    }
}
